package com.controller;

import com.entity.Product;
import com.entity.ProductEs;
import com.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("")
    public String index() {

        return "/product/index";

    }

    @RequestMapping("/insert")
    public String insert(Product product) {

        productService.insert(product);
        return "redirect:/product";

    }

    @RequestMapping("/search")
    public String viewSearch() {
        return "/product/search";
    }

    @RequestMapping("/dosearch")
    public String searchProductsByEs(String keyword, Model model) {
        List<ProductEs> list = productService.searchProductsByEs(keyword);
        model.addAttribute("list", list);
        return "/product/search";
    }

    @RequestMapping("/detail")
    public String getById(int id,Model model) {
        Product product = productService.getById(id);
        model.addAttribute("product", product);
        return "/product/detail";
    }
}
